package com.fsnbigdata;

import com.fsnbigdata.util.PublicStaticData;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.MultipartConfigFactory;
import org.springframework.boot.web.support.ServletContextApplicationContextInitializer;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.MultipartConfigElement;


/**
 * FsnbigdataApplication
 *
 * TODO Auto-generated class documentation
 *
 */
@SpringBootApplication
public class FsnbigdataApplication extends SpringBootServletInitializer {

    public static void main (String[] args) {
        //初始化
        PublicStaticData.init();
        SpringApplication.run(FsnbigdataApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        PublicStaticData.init();
        return application.sources(FsnbigdataApplication.class);
    }

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedHeaders("*")
                        .allowedMethods("OPTIONS", "POST", "GET", "HEAD", "PUT", "DELETE")
                        .allowedOrigins("*");
            }
        };
    }

    @Bean
    public MultipartConfigElement multipartConfigElement(){
        MultipartConfigFactory config = new MultipartConfigFactory();
        config.setMaxFileSize("10MB");
        config.setMaxRequestSize("12MB");
        return config.createMultipartConfig();
    }

//    @Bean
//    public EmbeddedServletContainerFactory servletContainerFactory() {
//        TomcatEmbeddedServletContainerFactory factory =
//            new TomcatEmbeddedServletContainerFactory();
//
//        factory.addConnectorCustomizers(
//            connector -> {
//                Http11NioProtocol protocol =
//                    (Http11NioProtocol)connector.getProtocolHandler();
//                protocol.setDisableUploadTimeout(true);
//                protocol.setConnectionTimeout(90000);
//                protocol.setKeepAliveTimeout(90000);
//            }
//        );
//
//        return factory;
//    }
}